package com.xyh.web;

import com.xyh.pojo.PageRequest;
import com.xyh.service.BlogService;
import com.xyh.service.TagService;
import com.xyh.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class IndexController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @GetMapping("/")
    public String index(PageRequest pageQuery, Model model) {
        model.addAttribute("page", blogService.getPage(pageQuery));
        model.addAttribute("types",typeService.getTypeTop(6));
        model.addAttribute("tags",tagService.getTagTop(6));
        model.addAttribute("recommendBlogs", blogService.getRecommendBlogsTop(8));
        return "index";
    }
    @GetMapping("/search")
    public String search(PageRequest pageRequest, @RequestParam String query, Model model){
        model.addAttribute("page", blogService.getPageSearch(pageRequest,query));
        model.addAttribute("query", query);
        return "search";
    }
    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id,Model model) {
        model.addAttribute("blog",blogService.getAndConvert(id));
        return "blog";
    }
    @GetMapping("/about")
    public String about() {
        return "about";
    }
    @GetMapping("/footer/newBlogs")
    public String newBlogs(Model model) {
        model.addAttribute("newBlogs", blogService.getRecommendBlogsTop(3));
        return "_fragments::newBlogList";
    }
}
